package com.rfid.a252robot.activity.presenter;

import android.os.Handler;

import com.rfid.a252robot.activity.view.IBookInfoView;
import com.rfid.a252robot.activity.view.IBorrowView;
import com.rfid.a252robot.base.BasePresenter;
import com.rfid.a252robot.event.BorrowEvent;
import com.rfid.a252robot.event.BoxCloseEvent;
import com.rfid.a252robot.event.BoxOpenEvent;
import com.rfid.a252robot.task.BorrowRestoreTask;
import com.rfid.common.log.LogUtils;
import com.rfid.manager.TaskManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BorrowPresenter extends BasePresenter<IBorrowView> {


    @Override
    public void onAttach(IBorrowView view) {
        super.onAttach(view);
        EventBus.getDefault().register(this);
    }

    @Override
    public void detach() {
        super.detach();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOpenBox(BoxOpenEvent event) {
        if (mView != null) {
            mView.onBoxOpen(event.getBoxNum(),event.isOpen());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBoxClose(BoxCloseEvent event) {
        LogUtils.v("onBoxClose = " + event);
        if (mView != null) {
            mView.onBoxClose(event.getBoxNum());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBorowBook(BorrowEvent event) {
        LogUtils.v("onBorowBook = " + event);
        if (mView != null) {
            mView.onBorrowBook(event.getBook());
        }
    }

    public String borrow(String reader,String pwd,int boxNum) {
        return TaskManager.instance().submitTask(new BorrowRestoreTask(boxNum,true,reader,pwd,new Handler()),false);
    }

}
